<template>
  <div>
    <MBX level1="数据统计" level2="数据报表"></MBX>
    <el-card>
      <div ref="box" class="box"></div>
    </el-card>
  </div>
</template>
<script>
import * as echarts from 'echarts'
import { zhexiantuApi } from '@/http/api'
console.log(echarts)
export default {
  name: 'reportsA',
  data() {
    return {
      myChart: null,
      // 公共数据
      preData: {
        title: {
          text: '用户来源'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEF3'
            }
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            boundaryGap: false
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ]
      }
    }
  },
  mounted() {
    this.shooo()
    // 通过调用echarts的resize实现自适应
    window.addEventListener('resize', this.mychart.resize)
  },
  methods: {
    async shooo() {
      // 获取dom
      const dom = this.$refs.box
      // 初始化
      this.mychart = echarts.init(dom)
      console.log(this.mychart)
      // 调用接口请求数据
      const res = await zhexiantuApi()
      console.log(res)
      //   配置
      const sumli = { ...this.preData, ...res }
      const options = sumli
      // 生成
      this.mychart.setOption(options)
    }
  }
}
</script>
<style lang="scss" scoped>
.box {
  width: 100%;
  height: 400px;
  border: 1px #f00 solid;
}
</style>
